<?php 

session_start();
require("../plain_config.php");

function generare_y($jumatate, $vector, $k) {
$i = 0;
$oprire = 0;
do {
$random_2 = rand(1,2);
if (($jumatate == "1") && ($random_2 == "1"))
 $y = rand(1, 150);
else
 $y = rand(151, 300);
 
if ($y != $vector[$i])
 $oprire = 1;
$i++;
} while(!$oprire);

return $y;
}

function generare_pozitie() {
for ($i=1; $i<=300; $i++) {
 $query = mysql_query("SELECT `id`, `".$i."` FROM `harta` WHERE `".$i."` != '0' AND `".$i."` != '1'AND `".$i."` != '2' AND `".$i."` != '3'");
 $nr = mysql_num_rows($query);	
   if ($nr > 0)
{
$n = rand(1,10)*10;
$prob = 100-$nr*100/300+$i/3;
if ($prob>$n)
 {
$k=0;
$suma = 1;
while($rez = mysql_fetch_array($query)) {
 $suma += $rez["id"];
 $vector[$k] = $rez["id"];
 $k++;
}
$ma = $suma/$k;

if ($ma <= 150)
$y = generare_y('1', $vector, $k);
 else
$y = generare_y('2', $vector, $k);

$x = $i;

break;
 }
}	
	else
	 {
	 $x = $i;
	 $y = rand(1, 300);
	 break;
	 }
  }
 return $x.'@'.$y;
}

if(isset($_GET['nume']) && isset($_GET['prenume']) && isset($_GET['utilizator']) && isset($_GET['parola']) && isset($_GET['email']))
{
$ip = $_SERVER['REMOTE_ADDR'];
$harta = rand(1,5);
$santier = rand(1, 16);
$id = md5(time().$ip);
$id_planeta = md5(time().$_GET['utilizator']);
$id_cladire = md5(time().$santier);
list($coord_x, $coord_y) = explode("@", generare_pozitie());
 
$cerere_users = "INSERT INTO users (id, user, pass, nume, prenume,  mail, llogin, avatar, ip, status, harta)
	          VALUES ('".$id."', '".addentities($_GET['utilizator'])."', '".md5($_GET['parola'])."', '".addentities($_GET['nume'])."', '".addentities($_GET['prenume'])."', '".addentities($_GET['email'])."', ' NOW () ', '".addentities($_GET['avatar'])."', '".$ip."', 'inactiv', '".$harta."')";
			  
$cerere_planeta = "INSERT INTO planete ( id, nume, coord_x, coord_y, user, minerale, gaz, energie ) VALUES ( '".$id_planeta."', '".addentities($_GET['utilizator'])."', '".$coord_x."', '".$coord_y."', '".addentities($_GET['utilizator'])."', '1000', '1000', '8000');";

$cerere_cladiri = "INSERT INTO cladiri (id, nume, santier, nivel, planeta) VALUES ('".$id_cladire."', '1', '".$santier."', '1', '".$id_planeta."')";
mysql_query("INSERT INTO unitati (tip, nr, locatie, planeta) VALUES ('1', '0', 'minerale', '".$id_planeta."')");
mysql_query("INSERT INTO unitati (tip, nr, locatie, planeta) VALUES ('1', '0', 'gaz', '".$id_planeta."')");
mysql_query("INSERT INTO unitati (tip, nr, locatie, planeta) VALUES ('0', '0', 'infrastructura', '".$id_planeta."')");
$cerere_harta = "UPDATE harta SET `".$coord_x."` = '".$id_planeta."' WHERE `id` = '".$coord_y."'";

//echo $coord_x.' '.$coord_y;
			  
mysql_query($cerere_users);
mysql_query($cerere_planeta);
mysql_query($cerere_cladiri);
mysql_query($cerere_harta);

}
else
echo '<meta http-equiv="refresh" content="0; url=../../index.php?eroare=403">';

?>